from django.http import JsonResponse
from django.shortcuts import render
from students.models import Students
from django.http import HttpResponse
import urllib.request
import urllib.parse

import json
from rest_framework import serializers
from rest_framework import generics
from wisdom_campus.settings import WECHAT_TOKEN, WECHAT_APPID, WECHAT_SECRET
# from django.core.cache import cache
import redis


def index(request):
    # 获取用户的code
    code = request.GET.get('code')
    # 拼接访问链接
    url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code' % (
        WECHAT_APPID, WECHAT_SECRET, code)
    # 获取链接
    resp = urllib.request.urlopen(url)
    # 转换链接
    json_str = resp.read()
    # 转换成json数据
    resp_dict = json.loads(json_str)
    # 判断是否有错  如果有  errcode 错误 抛出异常
    if 'errcode' in resp_dict:
        return HttpResponse('没有获取到用户信息')
    # 获取用户acces——token
    access_token = resp_dict.get('access_token')
    # 获取用户的id
    openid = resp_dict.get('openid')
    print('access_tokone%s,openid%s' % (access_token, openid))
    # 将用户的access_token存入redis数据库中  并设置过期时间
    # print(cache.get(openid),'SGg')
    print(openid, 'openid')
    caches = redis.StrictRedis(host='49.234.237.167', port=6379, password='redisredis', db=1)
    if caches.get(openid):
        return HttpResponse('用户信息已存在')
    else:

        caches.set(openid, access_token, 60 * 60)
        # 拼接链接
        url = 'https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN' % (access_token, openid)
        resps = urllib.request.urlopen(url)
        uer_json = resps.read()
        # 获取用户的数据
        uer_json_data = json.loads(uer_json)
        print(uer_json_data, 'data')
        if 'errcode' in uer_json_data:
            # return '没有获取到用户信息2'
            return HttpResponse('没有获取到用户信息2')
        else:
            # return HttpResponse('没有获取到用户信息1')
            return render(request, 'test.html')


"""


:return: 
"""

from werobot import WeRoBot

robot = WeRoBot(enable_session=False,
                token='zys',
                APP_ID='wx777ffd3540f72b23',
                APP_SECRET='f2be84624d4432a0fd817567ffb65eff')


@robot.handler
def hello(message):
    return 'Hello world'


"""
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx777ffd3540f72b23&redirect_uri=http%3A//8geguy.natappfree.cc%2Foauth_response.php&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx777ffd3540f72b23&redirect_uri=http%3A//pwrz6t.natappfree.cc/robot/stu&response_type=code&scope=snsapi_userinfo&state=2#wechat_redirect
"""
from students.serializers import StudentsSerializers
# class StudentsViews(generics.ListCreateAPIView):
#     queryset = Students.objects.filter(id=6)
#     serializer_class = StudentsSerializers
from rest_framework.views import APIView


class StudentsViews(generics.ListCreateAPIView):
    queryset = Students.objects.all()
    serializer_class = StudentsSerializers


